package com.roundarch.www.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.roundarch.www.DataEntity;
import com.roundarch.www.dao.EntityDao;
import com.roundarch.www.model.Files;

public class EntityDaoImpl extends HibernateDaoSupport implements EntityDao{

	public <T extends DataEntity> T find(long id,Class<T> c) {
		T t = getHibernateTemplate().get(c , id);
		return t;
	}

	public <T extends DataEntity> List<T> listAll(Class<T> c) {
		List<T> t = getHibernateTemplate().loadAll(c);
		return t;
	}

	public <T extends DataEntity> void save(T file) {
		getHibernateTemplate().saveOrUpdate(file);
	}

	public <T extends DataEntity> void delete(long id,Class<T> c) {
		getHibernateTemplate().delete(find(id,c));
	}
	
	@SuppressWarnings("unchecked")
    public <T extends DataEntity> T find(Class<T> entityClass, String propertyName, Object propertyValue)
    {
        List<T> t = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(entityClass).add(Restrictions.eq(propertyName, propertyValue)));
        return t.get(0);
    }
	

}
